package com.liuchuanzheng.lcz_study_demo.hook;

import android.content.Intent;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 替换IActivityManager的代理类
 */
public class IActivityManagerProxy implements InvocationHandler {

    private static final String TAG = "IActivityManagerProxy";

    private Object mActivityManager;


    public IActivityManagerProxy(Object activityManager) {
        this.mActivityManager = activityManager;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        if ("startActivity".equals(method.getName())) {//1
            Intent intent = null;

            int index = 0;

            for (int i = 0; i < args.length; i++) {
                if (args[i] instanceof Intent) {
                    index = i;
                    break;
                }
            }

            intent = (Intent) args[index];

            Intent subIntent = new Intent();//2
            String packageName = "com.liuchuanzheng.lcz_study_demo";
            subIntent.setClassName(packageName, packageName + ".hook.StubActivity");//3
            subIntent.putExtra(HookHelper.TARGET_INTENT, intent);//4
            args[index] = subIntent;//5

        }

        return method.invoke(mActivityManager, args);
    }
}

